<link rel="stylesheet" href="${request.contextPath}/static/oms/default/plugins/zTree/css/zTreeStyle/zTreeStyle.css" type="text/css">
<link href="${request.contextPath}/static/oms/iframe/plugins/bootstrap-icon-picker/css/icon-picker.css"  rel="stylesheet" type="text/css" />
<div class="row">
	<div class="col-md-12">
		<form id="edit-form" class="form-horizontal" action="${request.contextPath}/cms/article/category/update" method="post" onsubmit="return false">
			<input type="hidden" id="id" name="id" value="${cmsArticleCategory.id}">
			<div class="box-body">
				<div class="form-group">
					<label id="articleCategoryNameLabel" for="articleCategoryName" class="col-sm-2 control-label">名称</label>
					<div class="col-sm-10">
						<input type="text" class="form-control" name="name" id="articleCategoryName" value="${cmsArticleCategory.name}" placeholder="输入名称...">
					</div>
				</div>
				<div class="form-group">
					<label for="articleCategoryUsername" class="col-sm-2 control-label">用户名</label>
					<div class="col-sm-4">
						<input type="text" class="form-control" name="username" value="${cmsArticleCategory.username}"  id="articleCategoryUsername" placeholder="输入用户名...">
					</div>
					<label id="articleCategoryParentIdLabel" for="articleCategoryParentId" class="col-sm-2  control-label">上级分类</label>
					<div class="col-sm-4">
						<div class="input-group">
							<input type="hidden" class="form-control" id="articleCategoryParentId" name="parentId"  value="${cmsArticleCategory.parentId}" >
							<input type="text" class="form-control"  id="articleCategoryParentName" value="" placeholder="选择上级分类..." autocomplete="off">
							<span class="input-group-addon"><i class="glyphicon glyphicon-remove" style="color: #FF0000;" id="articleCategoryParentIdClear"></i></span>
							<div id="articleCategoryContent" class="articleCategoryContent" style="position: absolute; left: 0px; top: 25px; display: none;z-index: 999999;">
								<ul id="articleCategoryParentTree" class="ztree" style="margin-top: 10px;
																border: 1px solid #d6e0df;
																background: #fbfbfb;
																width: 220px;
																height: 360px;
																overflow-y: scroll;
																overflow-x: auto; -moz-user-select: none;">
								</ul>
							</div>
						</div>
					</div>
				</div>
				<div class="form-group">
					<label id="articleCategorySortOrderLabel" for="articleCategorySortOrder" class="col-sm-2  control-label">排序号</label>
					<div class="col-sm-4">
						<input class="form-control" name="sortOrder" id="articleCategorySortOrder" type="number" value="${cmsArticleCategory.sortOrder}" placeholder="输入排序号...">
					</div>
					<label id="articleCategoryStatusLabel" for="articleCategoryStatus" class="col-sm-2  control-label">状态</label>
					<div class="col-sm-4">
						<select class="form-control" id="articleCategoryStatus"  name="status" value="${cmsArticleCategory.status}" style="width: 100%;" placeholder="选择状态...">
						</select>
					</div>
				</div>
				<div class="form-group">
					<label id="articleCategoryRemarkLabel" class="col-sm-2 control-label">备注</label>
					<div class="col-sm-10">
						<input type="text" class="form-control" name="remark" id="articleCategoryRemark" value="${cmsArticleCategory.remark}"  placeholder="输入备注...">
					</div>
				</div>
				<div class="error-message text-center" >
					<span class="error-icon"></span>
					<span id="error-msg" class="error-msg" ></span>
				</div>
             </div>
			<div class="box-footer">
				<div class="pull-right">
               		<button type="submit" class="btn btn-primary btn-sm"><i class="fa fa-paste"></i> 更新</button>
               		<button type="button" class="btn btn-default btn-sm" data-dismiss="modal"><i class="fa fa-close"></i> 关闭</button>
				</div>
          	</div>
		</form>
	</div>
</div>
<script type="text/javascript" src="${request.contextPath}/static/oms/default/plugins/zTree/jquery.ztree.all.js"></script>
<script type="text/javascript" src="${request.contextPath}/static/oms/iframe/plugins/bootstrap-icon-picker/js/iconPicker.js"></script>

<script type="text/javascript">
    var setting = {
        data: {
            simpleData: {
                enable: true,
                idKey: "id",
                pIdKey: "parentId",
                rootPId: null
            }
        },
        async: {
            enable: true,
            url:"/cms/article/category/treeGrid",
            dataFilter:function (treeId, parentNode, responseData) {
                if (responseData) {
                    for(var i =0; i < responseData.length; i++) {
                        responseData[i].url = null;
                    }
                }
                return responseData;
            }
        },
        callback:{
            onAsyncSuccess:function (event, treeId) {
                var node = $.fn.zTree.getZTreeObj(treeId).getNodeByParam("id", $("#articleCategoryParentId").val());
                if(node){
                    $("#articleCategoryParentName").val(node.name);
                }
            },
            onClick :function (event, treeId, treeNode) {
                $("#articleCategoryParentId").val(treeNode.id);
                $("#articleCategoryParentName").val(treeNode.name);
                $('#articleCategoryContent').hide();
            }
        }
    };
    $(function() {
        $("#articleCategoryStatus").select2({
            data: StatusStore,
            language: "zh-CN",
            theme: "bootstrap"
        }).val('${cmsArticleCategory.status}').trigger("change");


		const $articleCategoryUsername = $("#articleCategoryUsername");

		$articleCategoryUsername.on('blur',function () {
			const $this = $(this);
			if($this.val()){
				setting.async.url="/cms/article/category/treeGrid?username="+$this.val();
				$.fn.zTree.init($('#articleCategoryParentTree'), setting);
			}
		});

        $("#articleCategoryParentName").on('click',function () {
            $('#articleCategoryContent').css("display")==='none'?$('#articleCategoryContent').show():$('#articleCategoryContent').hide();
        });
        $("#articleCategoryContent").mouseleave(function () {
            $('#articleCategoryContent').hide();
        });

        $("#articleCategoryParentIdClear").on('click',function () {
            $("#articleCategoryParentId").val("");
            $("#articleCategoryParentName").val("");
        });

        function ajaxSubmit(form) {

			const $form = $(form);

			if ($form.length === 1 && $form.validate()) {
                $.post(form.action, $form.serialize(),
                    function(result) {
                        if (result.success) {
                            $("#lgModal").modal('hide');
                            alertMsg(result.message,"success");
                            window.location.reload();
                        } else {
                            $form.find(".error-msg").show();
                            $form.find(".error-msg").html(result.message);
                        }
                    }, 'json');
            }
        }

		const $editForm = $("#edit-form");
		$editForm.validate({
            rules: {
                name: {
                    required: true
                }
            },
            messages: {
                name:{
                    required:"名称不能为空！"
                }
            },
            submitHandler:ajaxSubmit,
            errorLabelContainer:".error-msg",
            wrapper:"li",
            showErrors:function(errorMap,errorList) {
                if(errorList.length>0){
                    $editForm.find(".error-msg").html(errorList[0].message);
                }else{
                    $editForm.find(".error-msg").html("");
                }
            },
            onkeyup: false
        });
    });
</script>